package com.amazon.inapp.purchasing;

import android.os.RemoteException;
import com.amazon.android.framework.exception.KiwiException;
import com.amazon.venezia.command.SuccessResult;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;

final class KiwiPurchaseResponseCommandTask extends KiwiBaseCommandTask
{
  private static final String COMMAND_NAME = "purchase_response";
  private static final String COMMAND_VERSION = "1.0";
  private static final String TAG = "KiwiPurchaseResponseCommandTask";

  KiwiPurchaseResponseCommandTask(String paramString)
  {
    super("purchase_response", "1.0", paramString);
  }

  protected void onSuccess(SuccessResult paramSuccessResult)
    throws RemoteException, KiwiException
  {
    if (Logger.isTraceOn())
      Logger.trace("KiwiPurchaseResponseCommandTask", "onSuccess");
    Map localMap = paramSuccessResult.getData();
    if (Logger.isTraceOn())
      Logger.trace("KiwiPurchaseResponseCommandTask", "data: " + localMap);
    String str1 = (String)localMap.get("errorMessage");
    String str2 = (String)localMap.get("userId");
    String str3 = (String)localMap.get("receipt");
    if (Logger.isTraceOn())
      Logger.trace("KiwiPurchaseResponseCommandTask", "onSuccess: errorMessage: \"" + str1 + "\" receipt: \"" + str3 + "\"");
    PurchaseResponse.PurchaseRequestStatus localPurchaseRequestStatus1 = PurchaseResponse.PurchaseRequestStatus.FAILED;
    if ((isNullOrEmpty(str1)) && (!isNullOrEmpty(str3)));
    while (true)
    {
      try
      {
        JSONObject localJSONObject = new JSONObject(str3);
        String str4 = localJSONObject.getString("orderStatus");
        try
        {
          PurchaseResponse.PurchaseRequestStatus localPurchaseRequestStatus3 = PurchaseResponse.PurchaseRequestStatus.valueOf(str4);
          localPurchaseRequestStatus2 = localPurchaseRequestStatus3;
          if (PurchaseResponse.PurchaseRequestStatus.SUCCESSFUL == localPurchaseRequestStatus2)
          {
            localReceipt2 = getReceiptFromReceiptJson(localJSONObject);
            if (!verifyReceipt(str2, localReceipt2, localJSONObject))
            {
              localPurchaseRequestStatus2 = PurchaseResponse.PurchaseRequestStatus.FAILED;
              localReceipt2 = null;
            }
            localReceipt1 = localReceipt2;
            postRunnableToMainLooper(new Runnable()
            {
              public void run()
              {
                PurchasingObserver localPurchasingObserver = PurchasingManager.getPurchasingObserver();
                if (Logger.isTraceOn())
                  Logger.trace("KiwiPurchaseResponseCommandTask", "About to invoke onPurchaseResponse with PurchasingObserver: " + localPurchasingObserver);
                if (localPurchasingObserver != null)
                {
                  if (Logger.isTraceOn())
                    Logger.trace("KiwiPurchaseResponseCommandTask", "Invoking onPurchaseResponse with " + this.val$purchaseResponse);
                  localPurchasingObserver.onPurchaseResponse(this.val$purchaseResponse);
                  if (Logger.isTraceOn())
                    Logger.trace("KiwiPurchaseResponseCommandTask", "No exceptions were thrown when invoking onPurchaseResponse");
                  ImplementationFactory.getRequestHandler().sendPurchaseResponseReceivedRequest(KiwiPurchaseResponseCommandTask.this.getRequestId());
                }
              }
            });
            return;
          }
        }
        catch (Exception localException)
        {
          if (Logger.isErrorOn())
            Logger.error("KiwiPurchaseResponseCommandTask", "Invalid order status " + str4);
          localPurchaseRequestStatus2 = PurchaseResponse.PurchaseRequestStatus.FAILED;
          continue;
        }
      }
      catch (JSONException localJSONException)
      {
        if (Logger.isErrorOn())
          Logger.error("KiwiPurchaseResponseCommandTask", "Error parsing receipt JSON: " + localJSONException.getMessage());
        localPurchaseRequestStatus2 = PurchaseResponse.PurchaseRequestStatus.FAILED;
        localReceipt1 = null;
        continue;
        Receipt localReceipt2 = null;
        continue;
      }
      PurchaseResponse.PurchaseRequestStatus localPurchaseRequestStatus2 = localPurchaseRequestStatus1;
      Receipt localReceipt1 = null;
    }
  }

  protected void sendFailedResponse()
  {
    postRunnableToMainLooper(new Runnable()
    {
      public void run()
      {
        PurchasingObserver localPurchasingObserver = PurchasingManager.getPurchasingObserver();
        PurchaseResponse localPurchaseResponse = new PurchaseResponse(KiwiPurchaseResponseCommandTask.this.getRequestId(), null, null, PurchaseResponse.PurchaseRequestStatus.FAILED);
        if (localPurchasingObserver != null)
        {
          if (Logger.isTraceOn())
            Logger.trace("KiwiPurchaseResponseCommandTask", "Invoking onPurchaseResponse with " + localPurchaseResponse);
          localPurchasingObserver.onPurchaseResponse(localPurchaseResponse);
        }
      }
    });
  }
}

/* Location:           E:\android\Toy Defense 2_dex2jar.jar
 * Qualified Name:     com.amazon.inapp.purchasing.KiwiPurchaseResponseCommandTask
 * JD-Core Version:    0.6.2
 */